package com.intelligent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intelligent.mapper.CustomerInfoMapper;
import com.intelligent.mapper.CustomerManagementMapper;
import com.intelligent.mapper.ReceiptWriteOffMapper;
import com.intelligent.mapper.ReceiptOrderMapper;
import com.intelligent.mapper.TUserMapper;
import com.intelligent.pojo.CustomerInfo;
import com.intelligent.pojo.CustomerManagement;
import com.intelligent.pojo.ReceiptWriteOff;
import com.intelligent.pojo.ReceiptOrder;
import com.intelligent.pojo.TUser;
import com.intelligent.service.ReceiptWriteOffService;
import com.intelligent.vo.ReceiptWriteOffPageReq;
import com.intelligent.vo.ReceiptWriteOffResp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.stream.Collectors;

@Service
public class ReceiptWriteOffServiceImpl extends ServiceImpl<ReceiptWriteOffMapper, ReceiptWriteOff>
        implements ReceiptWriteOffService {

    @Resource
    private CustomerManagementMapper customerManagementMapper;

    @Resource
    private CustomerInfoMapper customerInfoMapper;

    @Resource
    private ReceiptOrderMapper receiptOrderMapper;

    @Resource
    private TUserMapper tUserMapper;

    @Override
    public Page<ReceiptWriteOffResp> page(ReceiptWriteOffPageReq req) {
        Page<ReceiptWriteOff> entityPage = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<ReceiptWriteOff> qw = new LambdaQueryWrapper<>();
        if (req.getWriteOffNo() != null && !req.getWriteOffNo().trim().isEmpty()) {
            qw.like(ReceiptWriteOff::getWriteOffNo, req.getWriteOffNo().trim());
        }
        if (req.getCustomerId() != null) {
            qw.eq(ReceiptWriteOff::getCustomerId, req.getCustomerId());
        }
        if (req.getReceiptOrderId() != null) {
            qw.eq(ReceiptWriteOff::getReceiptOrderId, req.getReceiptOrderId());
        }
        qw.orderByDesc(ReceiptWriteOff::getId);
        this.page(entityPage, qw);

        Page<ReceiptWriteOffResp> voPage = new Page<>(entityPage.getCurrent(), entityPage.getSize(), entityPage.getTotal());
        voPage.setRecords(entityPage.getRecords().stream().map(this::toResp).collect(Collectors.toList()));
        return voPage;
    }

    @Override
    public ReceiptWriteOffResp detail(Long id) {
        ReceiptWriteOff w = this.getById(id);
        return w == null ? null : toResp(w);
    }

    @Override
    public boolean create(ReceiptWriteOff writeOff) {
        // 自动生成核销流水号（如果表约束需要，不作为前端展示）
        if (writeOff.getWriteOffNo() == null || writeOff.getWriteOffNo().trim().isEmpty()) {
            String ts = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            writeOff.setWriteOffNo("HX" + ts);
        }
        if (writeOff.getWriteOffDate() == null) {
            writeOff.setWriteOffDate(new Date());
        }
        if (writeOff.getCreateTime() == null) {
            writeOff.setCreateTime(new Date());
        }
        return this.save(writeOff);
    }

    private ReceiptWriteOffResp toResp(ReceiptWriteOff w) {
        ReceiptWriteOffResp r = new ReceiptWriteOffResp();
        r.setId(w.getId());
        r.setWriteOffNo(w.getWriteOffNo());
        r.setCustomerId(w.getCustomerId());
        r.setReceiptOrderId(w.getReceiptOrderId());
        r.setReceivableDetailId(w.getReceivableDetailId());
        r.setAmount(w.getAmount());
        r.setWriteOffDate(w.getWriteOffDate());
        r.setUId(w.getUId());
        r.setRemark(w.getRemark());
        r.setCreateTime(w.getCreateTime());

        if (w.getReceiptOrderId() != null) {
            ReceiptOrder ro = receiptOrderMapper.selectById(w.getReceiptOrderId());
            if (ro != null) r.setReceiptOrderNo(ro.getReceiptOrderNo());
        }

        if (w.getCustomerId() != null) {
            CustomerManagement cm = customerManagementMapper.selectById(w.getCustomerId());
            if (cm != null) {
                r.setCustomerName(cm.getCustomerName());
            } else {
                CustomerInfo ci = customerInfoMapper.selectById(w.getCustomerId());
                if (ci != null && ci.getCustomerId() != null) {
                    CustomerManagement cm2 = customerManagementMapper.selectById(ci.getCustomerId());
                    if (cm2 != null) r.setCustomerName(cm2.getCustomerName());
                }
            }
        }

        if (w.getUId() != null) {
            TUser user = tUserMapper.selectById(w.getUId());
            if (user != null) r.setFinanceUserName(user.getRealName() != null ? user.getRealName() : user.getUsername());
        }
        return r;
    }
}
